package ar.com.solidussource.howmuchmobile.services.helper;

import ar.com.solidussource.howmuchmobile.common.entities.*;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created with IntelliJ IDEA.
 * User: FT31270775
 * Date: 21/11/12
 * Time: 17:02
 * To change this template use File | Settings | File Templates.
 */
public class RestServiceEntityExtractorHelper {

    public Geopoint getGeopoint(JSONObject jsonObject) throws JSONException {
        Geopoint geopoint = new Geopoint();

        geopoint.setLatitude(jsonObject.getDouble("latitude")*1E6);
        geopoint.setLongitude(jsonObject.getDouble("longitude")*1E6);

        return geopoint;
    }

    public Address getAddress(JSONObject jsonObject) throws JSONException {
        Address address = new Address();

        address.setName(jsonObject.getString("name"));

        if(jsonObject.getString("description") != null && !jsonObject.getString("description").equals("null")) {
            address.setDescription(jsonObject.getString("description"));
        }

        address.setZipcode(jsonObject.getString("zipcode"));

        address.setGeopoint(getGeopoint(jsonObject.getJSONObject("geopoint")));

        address.setLocation(getLocation(jsonObject.getJSONObject("location")));

        return address;
    }

    public Location getLocation(JSONObject jsonObject) throws JSONException {
        Location location = new Location();

        location.setId(jsonObject.getInt("id"));
        location.setName(jsonObject.getString("name"));
        location.setProvince(getProvince(jsonObject.getJSONObject("province")));

        return location;
    }

    public Province getProvince(JSONObject jsonObject) throws JSONException {
        Province province = new Province();

        province.setId(jsonObject.getInt("id"));
        province.setName(jsonObject.getString("name"));

        return province;
    }

    public Marketcategory getMarketcategory(JSONObject jsonObject) throws JSONException {
        Marketcategory marketcategory = new Marketcategory();

        marketcategory.setId(jsonObject.getInt("id"));
        marketcategory.setName(jsonObject.getString("name"));

        if(jsonObject.getString("description") != null && !jsonObject.getString("description").equals("null")) {
            marketcategory.setDescription(jsonObject.getString("description"));
        }

        return marketcategory;
    }

    public Market getMarket(JSONObject jsonObject) throws JSONException {
        Market market = new Market();

        market.setName(jsonObject.getString("name"));

        if(jsonObject.getString("phone") != null) {
            market.setPhone(jsonObject.getString("phone"));
        }

        if(jsonObject.getString("email") != null) {
            market.setEmail(jsonObject.getString("email"));
        }

        market.setScore(jsonObject.getLong("score"));

        if(jsonObject.getString("businesshour") != null && !jsonObject.getString("businesshour").equals("null")) {
            market.setBusinesshour(jsonObject.getString("businesshour"));
        }

        if(jsonObject.getString("twentyfourhourservice") != null && !jsonObject.getString("twentyfourhourservice").equals("null")) {
            market.setTwentyfourhourservice(jsonObject.getBoolean("twentyfourhourservice"));
        }

        market.setMarketcategory(getMarketcategory(jsonObject.getJSONObject("marketcategory")));
        market.setAddress(getAddress(jsonObject.getJSONObject("address")));

        return market;
    }
}
